package com.briup.server;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.briup.entity.Environment;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Collection;
import java.util.Properties;

public class test {
    public static void main(String[] args) throws Exception {
        Collection<Environment> receive = new ServerImpl().receive();
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        String username = "briup";
        String password = "briup";
        Connection conn = null;
        PreparedStatement ps = null;

        for (Environment environment : receive) {
            Timestamp time = environment.getTime();
//            System.out.println(time);
//            System.out.println(time.getDate());
            int date = time.getDate();

//            Properties prop = new Properties();
//            prop.load(new FileInputStream("D:\\env_project\\server\\src\\main\\resources\\druid.properties"));
//            DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//            Connection conn = dataSource.getConnection();
//            String tab = "env_detail_"+date;
//            String s = tab.toUpperCase();
//            System.out.println(s);
//            String sql = "insert into "+s+" values(?,?,?,?,?,?,?,?,?)";
//            PreparedStatement ps = conn.prepareStatement(sql);
//            ps.setString(1, environment.getName());
//            ps.setString(2, environment.getSrcId());
//            ps.setString(3, environment.getDesId());
//            ps.setString(4, environment.getAddress());
//            ps.setInt(5, environment.getCount());
//            ps.setString(6, environment.getCmd());
//            ps.setInt(7, environment.getStatus());
//            ps.setFloat(8, environment.getData());
//            ps.setTimestamp(9, environment.getTime());
//            int i = ps.executeUpdate();
//            System.out.println(i > 0);
//
//            ps.close();
//            conn.close();

            try {
                conn = DriverManager.getConnection(url, username, password);
                String tab = "env_detail_" + date;
                String s = tab.toUpperCase();

//            System.out.println(s);
                String sql = "insert into "+s+" values(?,?,?,?,?,?,?,?,?)";
                ps = conn.prepareStatement(sql);
                ps.setString(1, environment.getName());
                ps.setString(2, environment.getSrcId());
                ps.setString(3, environment.getDesId());
                ps.setString(4, environment.getAddress());
                ps.setInt(5, environment.getCount());
                ps.setString(6, environment.getCmd());
                ps.setInt(7, environment.getStatus());
                ps.setFloat(8, environment.getData());
                ps.setTimestamp(9, environment.getTime());
                int i = ps.executeUpdate();
//                System.out.println(i > 0);
                conn.close();
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
//            finally {
//                if (conn != null) {
//                    try {
//                        conn.close();
//                    } catch (SQLException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//                if (ps != null) {
//                    try {
//                        ps.close();
//                    } catch (SQLException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//            }


        }
    }
}

